/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets.model;

import java.io.Serializable;
import java.util.*;

/**
 * Models a taxonomy category
 *
 * @version 1.0
 * @author August Detlefsen for CodeMagi, Inc.
 */
public class Category extends BaseBean implements Serializable {

    private String description;
    private String alias;
    private Integer sortOrder;
    private Boolean allowMultiple = new Boolean(true);
    private Boolean allowFreeTags = new Boolean(false);
    private ArrayList terms       = new ArrayList();


    //GETTERS AND SETTERS -------------------------------------------------

    public void setDescription(String newValue) {
        description = newValue;
    }

    public String getDescription() {
        return description;
    }

    public void setAlias(String newValue) {
        alias = newValue;
    }

    public String getAlias() {
        return alias;
    }

    public void setSortOrder(Integer newValue) {
        sortOrder = newValue;
    }

    public Integer getSortOrder() {
        return sortOrder;
    }

    public void setAllowMultiple(Boolean newValue) {
	//no nulls allowed
	if (newValue != null) allowMultiple = newValue;
    }

    public Boolean getAllowMultiple() {
        return allowMultiple;
    }

    public void setAllowFreeTags(Boolean newValue) {
	//no nulls allowed
	if (newValue != null) allowFreeTags = newValue;
    }

    public Boolean getAllowFreeTags() {
        return allowFreeTags;
    }

    public void setTerms(ArrayList newValue) {
        if (newValue != null) terms = newValue;
    }

    public ArrayList getTerms() {
        return terms;
    }


    //SPECIAL GETTERS AND SETTERS -------------------------------------

    public void addTerm(Term term) {
	terms.add(term);
    }

    public void removeTerm(Term term) {
	terms.remove(term);
    }

    public boolean allowMultiple() {
	return allowMultiple.booleanValue();
    }

    public boolean allowFreeTags() {
	return allowFreeTags.booleanValue();
    }

    public Term getTerm(Integer termId) {
	Iterator i = terms.iterator();
	while (i.hasNext()) {
	    Term t = (Term)i.next();

	    if (t.getId().equals(termId)) return t;
	}

	return null;
    }

    /**
     * Gets a term by term name
     */
    public Term getTerm(String name) {
	Iterator i = terms.iterator();
	while (i.hasNext()) {
	    Term t = (Term)i.next();

	    if (t.getName().equals(name)) return t;
	}

	return null;
    }

    /** 
     * Returns a List of term names
     */
    public Set<String> getTermNames() {
	Set<String> output = new HashSet<String>(terms.size());
	Iterator i = terms.iterator();
        while (i.hasNext()) {
            Term t = (Term)i.next();

            output.add( t.getName() );
	}

	return output;
    }

}
